<template>
  <div class="resourse">
    <el-button type="primary" @click="showDialog(0)">
      添加
    </el-button>
    <!-- 展示数据的表格 -->
    <el-table
      :data="resourseList"
      style="width: 100%">
      <el-table-column
        prop="title"
        label="名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="code"
        label="提取码"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
           <el-button
           type="primary"
           icon="el-icon-edit"
           circle
           @click="showDialog(scope.row.id)"
           ></el-button>
           <el-button
           type="danger"
           icon="el-icon-delete"
           circle
           @click="deleteResourse(scope.row.id)"
           ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible" @close='dialogClose'>
      <resourse-form :resourse="resourse"></resourse-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveContent">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getResourseList, deleteResourse, createResourse, selectResourseById, updateResourse } from '@/api/resourse'
import ResourseForm from './ResourseForm.vue'
export default {
  name: 'resourse',
  components: {
    'resourse-form': ResourseForm
  },
  data () {
    return {
      resourseList: [],
      resourse: {
        title: '',
        code: 0,
        url: ''
      },
      isUpdate: 0,
      dialogFormVisible: false
    }
  },
  created () {
    this.LoadResourseList()
  },
  methods: {
    async LoadResourseList () {
      const { data: res } = await getResourseList()
      this.resourseList = res.data.resourseList
    },
    // 删除资源
    deleteResourse (id) {
      this.$confirm('你确定删除该资源吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async () => {
        const { data: res } = await deleteResourse(id)
        if (res.code === 20000) {
          this.$message.success('数据已成功删除')
          this.LoadResourseList()
        } else {
          this.$message.error('数据删除失败')
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 添加资源
    async addResourse () {
      const { data: res } = await createResourse(this.resourse)
      if (res.code === 20000) {
        this.$message.success('添加数据成了！')
        this.LoadResourseList()
      } else {
        this.$message.error('添加数据失败')
      }
    },
    // 显示对话框
    async showDialog (id) {
      this.isUpdate = id
      if (id) {
        const { data: res } = await selectResourseById(this.isUpdate)
        this.resourse = res.data
      }
      this.dialogFormVisible = true
    },
    // 保存数据事件
    async saveContent () {
      if (this.isUpdate) {
        const { data: res } = await updateResourse(this.isUpdate, this.resourse)
        if (res.code === 20000) {
          this.$message.success('修改成功')
          this.LoadResourseList()
          this.dialogFormVisible = false
        } else {
          this.$message.error('修改失败')
        }
      } else {
        this.addResourse()
        this.dialogFormVisible = false
      }
    },
    dialogClose () {
      this.resourse = {
        title: '',
        code: 0,
        url: ''
      }
    }
  }
}
</script>
